class GetHttp {
  static baseUrl = `http://121.37.198.213:`
  static request(url) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.baseUrl + url,
        data: '',
        dataType: 'json',
        enableCache: true,
        enableHttp2: true,
        enableQuic: true,
        header: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' },
        scriptCharset: 'utf-8',
        method: 'GET',
        responseType: 'text',
        timeout: 0,
        success: (result) => { resolve(result) },
        fail: (res) => { reject(res) },
        complete: (res) => { },
      })
    })
  }
}

//主页接口get
class getHomeHttp extends GetHttp {
  //城市
  static getCityHttp() {
    return this.request(`7000/city`)
  }
  //轮播
  static getDrinkBannerHttp() {
    return this.request(`7000/drink/banner`)
  }
  //轮播详情
  static getDrinkBannerDetailHttp(id) {
    return this.request(`7000/drink/banner/${id}`)
  }
  //推荐商品
  static getHotDrinkHttp() {
    return this.request(`7000/drink/hot`)
  }
}

//社区get
class getSquareHttp extends GetHttp {
  //社区晒单
  static getUserSquareHttp() {
    return this.request(`7000/square`)
  }
  //社区用户
  static getUserMsgHttp(id) {
    return this.request(`7000/square/${id}`)
  }
}

//全部商品get
class getWaresHttp extends GetHttp {
  //全部商品
  static getDrinkHttp(index) {
    return this.request(`7000/drink?offset=${index}&limit=20`)
  }
  //搜索商品
  static getWaresSearch(value) {
    return this.request(`7000/wares?keyword=${value}`)
  }
  //商品详情
  static getDrinkDetailHttp(id) {
    return this.request(`7000/drink/${id}`)
  }
  //券
  static getTicketHttp() {
    return this.request(`7000/allowance`)
  }
}

//购物车get
class getShoppingHttp extends GetHttp {
  //购物车商品
  static getShoppingGoodsHttp() {
    return this.request(`7000/shopping/car`)
  }
}

//******post */
class PostHttp {
  static baseUrl = `http://121.37.198.213:`
  static request({ url, data }) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.baseUrl + url,
        data: data,
        dataType: 'json',
        enableCache: true,
        enableHttp2: true,
        enableQuic: true,
        header: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' },
        scriptCharset: 'utf-8',
        method: 'POST',
        responseType: 'text',
        timeout: 0,
        success: (result) => { resolve(result) },
        fail: (res) => { reject(res) },
        complete: (res) => { },
      })
    })
  }
}
//全部商品post
class postWaresHttp extends PostHttp {
  //商品收藏
  static postWaresCollectHttp({ id, collected }) {
    return this.request({
      url: `7000/drink/collect`,
      data: { id, collected }
    })
  }
  //后台编辑优惠券（这里不需要）
  static postReceiveTicket() {
    return this.request({ url: `7000/allowance/getcoupons`, data: {} })
  }
}

//社区post
class postSquareHttp extends PostHttp {
  //关注
  static postAttentionHttp({ id, collected }) {
    return this.request({
      url: `7000/attention/collect`,
      data: { id, collected }
    })
  }
}

//购物车post
class postShoppingHttp extends PostHttp {
  //商品加购  
  static postShoppingGoodsHttp(data) {
    return this.request({
      url: `7000/shopping/addbuy`,
      data
    })
  }
}

function getCityAddress(url) {
  return (() => {
    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        data: '',
        dataType: 'json',
        enableCache: true,
        enableHttp2: true,
        enableQuic: true,
        header: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' },
        scriptCharset: 'utf-8',
        method: 'GET',
        responseType: 'text',
        timeout: 0,
        success: (result) => { resolve(result) },
        fail: (res) => { reject(res) },
        complete: (res) => { },
      })
    })
  })(url)
}
module.exports = {
  getHomeHttp, getSquareHttp, getWaresHttp, getShoppingHttp,
  postWaresHttp, postSquareHttp, postShoppingHttp, getCityAddress
}
